using System.Configuration;
using System.Web.Mvc;

namespace WineCellar.Web.Helpers
{
    public class WineCellarWebFormViewEngine :WebFormViewEngine
    {
        public WineCellarWebFormViewEngine() 
        {
            var section = ConfigurationManager.GetSection("winecellarmanager.Tenants") as TenantConfigurationSection;
            if (section != null && section.TenantName != null)
            {
                MasterLocationFormats = new[] 
                                            {
                                                "~/Tenants/" + section.TenantName + "/Views/{1}/{0}.master",
                                                "~/Tenants" + section.TenantName + "/Views/Shared/{0}.master",
                                                "~/Views/{1}/{0}.master",
                                                "~/Views/Shared/{0}.master"
                                            };

                ViewLocationFormats = new[] 
                                          {
                                              "~/Tenants/" + section.TenantName + "/Views/{1}/{0}.aspx",
                                              "~/Tenants/" + section.TenantName + "/Views/{1}/{0}.ascx",
                                              "~/Tenants/" + section.TenantName + "/Views/Shared/{0}.aspx",
                                              "~/Tenants/" + section.TenantName + "/Views/Shared/{0}.ascx",
                                              "~/Views/{1}/{0}.aspx",
                                              "~/Views/{1}/{0}.ascx",
                                              "~/Views/Shared/{0}.aspx",
                                              "~/Views/Shared/{0}.ascx"
                                          };   
            }
            else //default view locations
            {
                MasterLocationFormats = new[] 
                                            {
                                                "~/Views/{1}/{0}.master",
                                                "~/Views/Shared/{0}.master"
                                            };

                ViewLocationFormats = new[] 
                                          {
                                              "~/Views/{1}/{0}.aspx",
                                              "~/Views/{1}/{0}.ascx",
                                              "~/Views/Shared/{0}.aspx",
                                              "~/Views/Shared/{0}.ascx"
                                          };   
            }            

            PartialViewLocationFormats = ViewLocationFormats;
        }
    }
}